﻿using Microsoft.Extensions.Logging;
using ST.Common.Util.Mvc;
using ST.WX.Common;
using ST.WX.Common.Enums;
using System;
using System.Collections.Generic;

namespace ST.WX.Common
{
	/// <summary>
	/// 应用管理器
	/// </summary>
	public class WXAppManager : IWXAppManager
	{
		private readonly ILogger logger;

		public WXAppManager(ILogger<WXAppManager> _logger)
		{
			logger = _logger;
		}

		private static Dictionary<string, WXAppConfig> appDic = new Dictionary<string, WXAppConfig>()
		{
			{"wxf2a1b892697dd2ab",
				new WXAppConfig("wxf2a1b892697dd2ab", "f2d3eff3f082e759cb5689609d3882fe", "E6A87C8F1CCF493F9918EFEB96ACA313", "YoGM6IxBU1wgSwgYgWfjGHEpLIrzw9gsocdENzRDb0T", "1369062002", "08EA3289AE30493CA4AE1D3DE0DCBFC8",WXAppType.MP,"yflt.nanjingruike.com")
				{
					AppName = "创达科技公众号"
				}
			}
			//,{"wxd9789321494c55cf",
			//	new WXAppConfig("wxd9789321494c55cf", "b172eb544ca67365aebc1da4458eabf0", "BBE495EDD96E4D799B0C2B58C736C198", "oxsOsw0Ir6pyOudDDnIjQhzffvZDV7lzVKvqiJ1Jizk",WXAppType.XCX,"app1.sqiter.top")
			//	{
			//		AppName = "创达科技小程序"
			//	}
			//}
		};

		/// <summary>
		/// 根据当前请求的域名获取第一个应用配置信息
		/// </summary>
		/// <returns></returns>
		public WXAppConfig GetAppDefault(WXAppType type = WXAppType.MP)
		{
			string domain = HttpContextHelper.Current.Request.Host.ToString();
			return GetAppByDomain(domain, type);
		}

		public WXAppConfig GetAppByID(string appID)
		{
			WXAppConfig config = null;
			try
			{
				if (string.IsNullOrEmpty(appID))
				{
					throw new WXException("请求的App未知");
				}
				if (appDic == null)
				{
					throw new WXException("注册的应用列表为空");
				}
				if (appDic.ContainsKey(appID))
				{
					config = appDic[appID];
				}
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException, ex.Message);
			}
			return config;
		}

		/// <summary>
		/// 根据域名获取第一个应用相关配置信息
		/// </summary>
		/// <param name="domain"></param>
		/// <returns></returns>
		public WXAppConfig GetAppByDomain(string domain)
		{
			WXAppConfig config = null;
			try
			{
				if (string.IsNullOrEmpty(domain))
				{
					throw new WXException("请求的域名无相关应用");
				}
				if (appDic == null)
				{
					throw new WXException("注册的应用列表为空");
				}
				foreach (WXAppConfig v in appDic.Values)
				{
					if (v.Domain == domain)
					{
						config = v;
						break;
					}
				}
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException, ex.Message);
			}
			return config;
		}

		/// <summary>
		/// 根据域名获取第一个指定类型应用的相关配置信息
		/// </summary>
		/// <param name="domain"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public WXAppConfig GetAppByDomain(string domain, WXAppType type)
		{
			WXAppConfig config = null;
			try
			{
				if (string.IsNullOrEmpty(domain))
				{
					throw new WXException("请求的域名无相关应用");
				}
				if (appDic == null)
				{
					throw new WXException("注册的应用列表为空");
				}
				foreach (WXAppConfig v in appDic.Values)
				{
					if (v.Domain == domain && v.AppType == type)
					{
						config = v;
						break;
					}
				}
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException, ex.Message);
			}
			return config;
		}

		public Dictionary<string, WXAppConfig> GetApps()
		{
			return appDic;
		}

		public bool AddApp(WXAppConfig appConfig, bool allowReregister = true)
		{
			try
			{
				if (appConfig == null || string.IsNullOrEmpty(appConfig.AppID))
				{
					throw new WXException("请求的App未知");
				}
				if (appDic == null)
				{
					throw new WXException("注册的应用列表为空");
				}
				if (!allowReregister && appDic.ContainsKey(appConfig.AppID))
				{
					throw new WXException("已注册了相同AppID的应用");
				}
				if (!appDic.ContainsKey(appConfig.AppID))
				{
					appDic.Add(appConfig.AppID, appConfig);
				}
				else
				{
					appDic[appConfig.AppID] = appConfig;
				}
				return true;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException, ex.Message);
			}
			return false;
		}

		public bool RemoveApp(WXAppConfig appConfig)
		{
			try
			{
				if (appConfig == null || string.IsNullOrEmpty(appConfig.AppID))
				{
					throw new WXException("待移除的应用信息不能为空");
				}
				return RemoveApp(appConfig.AppID);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
			}
			return false;
		}

		public bool RemoveApp(string appID)
		{
			try
			{
				if (appDic == null)
				{
					throw new WXException("注册的应用列表为空");
				}
				if (string.IsNullOrEmpty(appID))
				{
					throw new WXException("待移除的应用AppID不能为空");
				}
				if (!appDic.ContainsKey(appID))
				{
					throw new WXException("已注册的应用列表未找到相关应用");
				}
				appDic.Remove(appID);
				return true;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
			}
			return false;
		}
	}

}
